package com.huaxindata.im.main.chat.Helper;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import com.bumptech.glide.Glide;
import com.huaxindata.im.constants.ImConstants;
import com.huaxindata.im.main.chat.VideoPlayerActivity;
import com.huaxindata.im.main.chat.adapter.ChatAdapter;
import com.huaxindata.im.main.manager.UserManager;
import com.huaxindata.im.model.entity.ChatMessage;
import com.huaxindata.im.model.entity.ChatVideoMessageBody;

import java.io.File;

/**
 * Created by Administrator on 2017/9/26.
 * 负责处理聊天界面视频显示逻辑
 */

public class VideoHelper {
    public static void handleVideoMsg(final Context context, final ChatMessage message, final ChatAdapter adapter, ChatAdapter.ViewHolder viewHolder){
        final ChatVideoMessageBody messageBody= (ChatVideoMessageBody) message.getMessageBody();
        //设置视频缩略图
        final String localUrl = messageBody.getLocalPath();
        final File localFile=new File(localUrl);
        if (localFile.exists()) {
            Glide.with(context).load(localUrl).thumbnail(0.1f).into(viewHolder.mVideoThumbnailIv);
        } else {
            Glide.with(context).load(messageBody.getRemotePath()).thumbnail(0.1f).into(viewHolder.mVideoThumbnailIv);
        }
        viewHolder.mVideoSizeTv.setText(messageBody.getFileSize(localUrl));
        viewHolder.mVideoLengthTv.setText(messageBody.getVideoLength(localUrl));
        viewHolder.mVideoPlayBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, VideoPlayerActivity.class);
                if (message.isSender(UserManager.getClientId())&&localFile.exists()) {//如果本地文件存在
                    //用exoPlayer播放
                    intent.putExtra(ImConstants.FILE_URL, localUrl);
                    intent.putExtra(ImConstants.FILE_URL_CATEGORY, ImConstants.LOCAL_FILE);
                } else {//如果本地文件已经删除，就去加载网络上的视频
                    intent.putExtra(ImConstants.FILE_URL, messageBody.getRemotePath());
                    intent.putExtra(ImConstants.FILE_URL_CATEGORY, ImConstants.REMOTE_FILE);
                    if (message.isSender(UserManager.getClientId())) {
                        intent.putExtra(ImConstants.CLIENT_ID, message.receiver);
                    } else {
                        intent.putExtra(ImConstants.CLIENT_ID, message.sender);
                    }
                }
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }
        });
        //如果发送失败或接收失败的处理
        viewHolder.mStatusIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (message.isSender(UserManager.getClientId())) {
                    FileHelper.uploadFileMessage(adapter,message);
                } else {
                    FileHelper.downLoadFileMessage(message,context,adapter);
                }
            }
        });
    }
}
